﻿using System.Collections.Generic;
using System.Drawing;

namespace MDAControlsFramework.Helpers {
	/// <summary>
	/// Skupina stylu. Stará se o schraňování, vykreslení a další práci s nimi
	/// </summary>
	public class MDAStyles: IMDAObject {
		private const string defaultStyleName = "default";

		// actual style nesmí být null
		private MDAStyle _actualStyle;
		private MDAStyle _selectedStyle = MDAStyle.None;
		private Dictionary<string, MDAStyle> _styles = new Dictionary<string, MDAStyle>(4);
		private string _name = "null";
		private MDAControl _control = null;

		public MDAStyles(MDAControl control) {
			_control = control;
			_actualStyle = new MDAStyle(defaultStyleName);
			_styles.Add(defaultStyleName, _actualStyle);
		}

		public object Clone() {
			MDAStyles clone = this.MemberwiseClone() as MDAStyles;
			return clone;
		}

		#region properties

		/// <summary>
		/// Styl nastavený jako aktuální, nikdy nebude null
		/// </summary>
		public MDAStyle ActualStyle {
			get {
				return _actualStyle;
			}
			set {
				_actualStyle = value;
			}
		}

		public MDAStyle SelectedStyle {
			get {
				return _selectedStyle;
			}
			set {
				_selectedStyle = value;
			}
		}
		/// <summary>
		/// Přidání stylu do seznamu, pod názvem stylu
		/// </summary>
		/// <param name="style">styl na přidání</param>
		public void AddStyle(MDAStyle style) {
			AddStyle(style.GetName(), style);
		}

		/// <summary>
		/// Přidání stylu do seznamu, pod definovaným názvem
		/// </summary>
		/// <param name="name">název, pod kterým se styl přidá</param>
		/// <param name="style">přidávaný styl</param>
		public void AddStyle(string name, MDAStyle style) {
			name = name.ToLower();
			if (_styles.ContainsKey(name)) {
				return;
			}
			if (_styles.Count == 1 && _actualStyle.Name == defaultStyleName) {
				// pridava se prvni styl 
				_actualStyle.CopyData(style);
				_styles = new Dictionary<string, MDAStyle>();
				_styles.Add(name, _actualStyle);
			} else {
				_styles.Add(name, style.DeepCopy());
			}
			if (style.Selected) {
				_selectedStyle = style;
			}
		}

		#endregion


		public bool AddObject(IMDAObject objectInstance) {
			// může obsahovat pouze styly
			MDAStyle style = objectInstance as MDAStyle;
			if (style != null) {
				this.AddStyle(style.GetName(), style);
				return true;
			}

			if (_actualStyle.AddObject(objectInstance)) {
				return true;
			}

			return false;
		}

		public bool SetProperty(string name, string value) {
			_actualStyle.SetProperty(name, value);
			return false;
		}

		public void AddEvent(string eventName, string eventHandler) {
			return;
		}

		public string GetName() {
			return _name;
		}

		public void SetActualStyle(string styleName) {
			styleName = styleName.ToLower();

			MDAStyle style;
			if (_styles.TryGetValue(styleName, out style)) {
				_actualStyle = style;
				return;
			}

			style = MDASettings.GetStyle(styleName);
			if (style != null) {
				style = style.DeepCopy();
				_styles.Add(styleName, style);
				_actualStyle = style;
			}
		}

		public MDAStyle GetStyle(string name) {
			MDAStyle style;
			if (_styles.TryGetValue(name, out style)) {
				return style;
			}
			return null;
		}

		#region Drawing

		public void Draw(Graphics g, Rectangle rectangle) {
			_actualStyle.DrawBackground(g, _control.Rectangle());
			_actualStyle.DrawImage(g, _control.Rectangle());
			_actualStyle.DrawText(g, _control.Rectangle());
			_actualStyle.DrawBorder(g, _control.Rectangle());
		}

		public void DrawSelected(Graphics g, Rectangle rectangle) {
			if (_selectedStyle != null) {
				_selectedStyle.DrawBackground(g, _control.Rectangle());
				_selectedStyle.DrawImage(g, _control.Rectangle());
				_selectedStyle.DrawText(g, _control.Rectangle());
				_selectedStyle.DrawBorder(g, _control.Rectangle());
			}
		}

		public void DrawBackground(Graphics g, Rectangle rectangle) {
			if (_actualStyle != null) {
				_actualStyle.DrawBackground(g, rectangle);
			}
		}

		public void DrawImage(Graphics g, Rectangle rectangle) {
			if (_actualStyle != null) {
				_actualStyle.DrawImage(g, rectangle);
			}
		}

		public void DrawText(Graphics g, Rectangle rectangle) {
			if (_actualStyle != null) {
				_actualStyle.DrawText(g, rectangle);
			}
		}

		public void DrawBorder(Graphics g, Rectangle rectangle) {
			if (_actualStyle != null) {
				_actualStyle.DrawBorder(g, rectangle);
			}
		}

		public void DrawSelectedBackground(Graphics g, Rectangle rectangle) {
			if (_selectedStyle != null) {
				_selectedStyle.DrawBackground(g, rectangle, _actualStyle);
			}
		}

		public void DrawSelectedImage(Graphics g, Rectangle rectangle) {
			if (_selectedStyle != null) {
				_selectedStyle.DrawImage(g, rectangle, _actualStyle);
			}
		}

		public void DrawSelectedText(Graphics g, Rectangle rectangle) {
			if (_selectedStyle != null) {
				_selectedStyle.DrawText(g, rectangle, _actualStyle);
			}
		}

		public void DrawSelectedBorder(Graphics g, Rectangle rectangle) {
			if (_selectedStyle != null) {
				_selectedStyle.DrawBorder(g, rectangle, _actualStyle);
			}
		}

		#endregion

		public override string ToString() {
			string vrat = " {Styles \"" + _name + "\" [";
			foreach (string style in _styles.Keys) {
				vrat += style + ": " + _styles[style] + ", ";
			}
			return vrat + "]}";
		}

		public string Keys() {
			string vrat = "";
			foreach (string style in _styles.Keys) {
				vrat += style + ", ";
			}
			return vrat;
		}
	}
}
